package com.oa.co.service.impl;
/*
 *                                                     __----~~~~~~~~~~~------___
 *                                    .  .   ~~//====......          __--~ ~~
 *                    -.            \_|//     |||\\  ~~~~~~::::... /~
 *                 ___-==_       _-~o~  \/    |||  \\            _/~~-
 *         __---~~~.==~||\=_    -_--~/_-~|-   |\\   \\        _/~
 *     _-~~     .=~    |  \\-_    '-~7  /-   /  ||    \      /
 *   .~       .~       |   \\ -_    /  /-   /   ||      \   /
 *  /  ____  /         |     \\ ~-_/  /|- _/   .||       \ /
 *  |~~    ~~|--~~~~--_ \     ~==-/   | \~--===~~        .\
 *           '         ~-|      /|    |-~\~~       __--~~
 *                       |-~~-_/ |    |   ~\_   _-~            /\
 *                            /  \     \__   \/~                \__
 *                        _--~ _/ | .-~~____--~-/                  ~~==.
 *                       ((->/~   '.|||' -_|    ~~-/ ,              . _||
 *                                  -_     ~\      ~~---l__i__i__i--~~_/
 *                                  _-~-__   ~)  \--______________--~~
 *                                //.-~~~-~_--~- |-------~~~~~~~~
 *                                       //.-~~~--\
 *                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 *                               神兽保佑            永无BUG
 */


import com.alibaba.druid.util.StringUtils;
import com.oa.co.common.R;
import com.oa.co.mapper.UserMapper;
import com.oa.co.pojo.User;
import com.oa.co.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author sunXuDong
 * @data 2023/5/3
 */
@Service("userService")
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public R login(String username, String password, HttpServletRequest request) {
        User user = userMapper.login(username);
        if (user!=null){
            if (password.equals(user.getPassword())){
                return R.ok().message("登录成功").data("user",user);
            }
            return R.error().message("密码错误");
        }
        return R.error().message("账号(用户名)错误");
    }

    @Override
    public R adduser(User user) {

        return  userMapper.addUser(user)==1 ? R.ok():R.error();
    }

    @Override
    public R rPwd(String username, String phone, String password, String passwordIs) {
        if (password.equals(passwordIs)){
          User user =  userMapper.rPwd(username,phone);
          if (user!=null){

              return userMapper.updatePassword(username,phone,password)==1? R.ok():R.error().message("系统出错了");
          }
             User user1 = userMapper.rPwd(username,null);
          if (user1!=null){
              return R.error().message("手机号码错误");
          }
          return R.error().message("用户名错误");
        }
        return R.error().message("两次密码不相同");
    }

    @Override
    public R upInfo(User user) {
        if (user!=null){
        Integer up = userMapper.upInfo(user);
        if (up==1){
            User user1 = userMapper.query(user.getId());
            return user1 !=null ? R.ok().data("info",user1).message("修改成功") : R.error().message("修改失败");
        }else {
            return R.error().message("修改失败");
        }
        }
        return R.error().message("修改失败");
    }

    @Override
    public R interiorPassword(String password, String passwordIs1, String passwordIs2,Integer id) {

        if (!StringUtils.isEmpty(password) && !StringUtils.isEmpty(passwordIs1) && !StringUtils.isEmpty(passwordIs2) && id!=null){
            if (passwordIs1.equals(passwordIs2)){
                User query = userMapper.queryByPwd(id,password);
                if (query!=null){
                    return userMapper.interiorupdatePassword(id, passwordIs1) == 1?R.ok().message("修改成功请重新登录") : R.error().message("修改失败");
                }
            }else {
                return R.error().message("新密码不相同");
            }
        }
        return R.error().message("系统错误");
    }
}
